PokeredGameDB._processTrades   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
// Import Dependencies
2
const _ = require("lodash");
3
4
// Load dependency from Window if in a browser or from module if in NodeJS enviroment
5
let PokeredMonDB;
6
if(typeof window === "undefined")
7
    PokeredMonDB = (require("pokered-mon-database")).PokeredMonDB;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
8
else
9
    PokeredMonDB = window.PokeredMonDB.PokeredMonDB;
10
11
const monDB = new PokeredMonDB();
12
13
// Import Raw JSON Files
14
const eventPokemonRaw = require("./data/eventPokemon.json");
15
const eventsRaw = require("./data/events.json");
16
const hiddenCoinsRaw = require("./data/hiddenCoins.json");
17
const hiddenItemsRaw = require("./data/hiddenItems.json");
18
const mapsRaw = require("./data/maps.json");
19
const missablesRaw = require("./data/missables.json");
20
const randomNamesRaw = require("./data/randomNames.json");
21
const scriptsRaw = require("./data/scripts.json");
22
const spritesRaw = require("./data/sprites.json");
23
const starterCandidatesRaw = require("./data/starterCandidates.json");
24
const startersRaw = require("./data/starters.json");
25
const textCodesRaw = require("./data/textCodes.json");
26
const townsRaw = require("./data/towns.json");
27
const tradesRaw = require("./data/trades.json");
28
const trainersRaw = require("./data/trainers.json");
29
const worldScriptsRaw = require("./data/worldScripts.json");
30
31
// Class Constructor
32
// Initiate Array
33
const PokeredGameDB = exports.PokeredGameDB = function() {
34
    // Do initial build
35
    this.rebuild();
36
}
37
38
// Selectively inherit PokeredMonDB class properties as it's own uniquely
39
PokeredGameDB.prototype = _.cloneDeep(PokeredMonDB.prototype);
40
delete PokeredGameDB.prototype._processAllItems;
41
delete PokeredGameDB.prototype._processAllMoves;
42
delete PokeredGameDB.prototype._processAllPokemon;
43
delete PokeredGameDB.prototype._processEvolutionEntry;
44
delete PokeredGameDB.prototype.itemsRaw;
45
delete PokeredGameDB.prototype.movesRaw;
46
delete PokeredGameDB.prototype.typesRaw;
47
delete PokeredGameDB.prototype.pokemonRaw;
48
49
// Build or Rebuild database
50
// This should normally never need to be called manually
51
// and is provided for special edge cases. Keep in mind calling this again
52
// might create memory leaks in certain browsers due to circular references.
53
PokeredGameDB.prototype.rebuild = function() {
54
55
    //
56
    // Pass 1: Indexing, Cloning, and Setup
57
    //
58
59
    this._indexMaps();
60
    this._autoIndex(this.missablesRaw, "missables");
61
    this.events = _.cloneDeep(eventsRaw);
62
    this.randomNames = _.cloneDeep(randomNamesRaw);
63
    this._indexScripts();
64
    this._autoIndex(this.spritesRaw, "sprites");
65
    this._indexTextCodes();
66
67
    //
68
    // Pass 2: Processing
69
    //
70
71
    this._processEventPokemon();
72
    this._processHidden(this.hiddenCoinsRaw, "hiddenCoins");
73
    this._processHidden(this.hiddenItemsRaw, "hiddenItems");
74
    
75
    this.starterCandidates = _.cloneDeep(this.starterCandidatesRaw);
76
    this._processArrayDBEntry(this, "starterCandidates", monDB.pokemon);
77
78
    this.starters = _.cloneDeep(this.startersRaw);
79
    this._processArrayDBEntry(this, "starters", monDB.pokemon);
80
    this._processTowns();
81
    this._processTrades();
82
    this._processTrainers();
83
}
84
85
// Creates a 2nd index prefixed which just aliases the first index
86
// Only creates alias if key exists
87
PokeredGameDB.prototype._indexSecondaryPrefix = function(from, to, key, origKey, prefix) {
88
    if(prefix === undefined)
89
        prefix = key;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
90
91
    this._iterate(from, function(entry) {
92
        if(entry[key] !== undefined)
93
            to['' + prefix + entry[key]] = to[entry[origKey]];
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
94
    });
95
}
96
97
PokeredGameDB.prototype._indexMaps = function() {
98
    this.maps = [];
99
100
    this._indexPrimary(this.mapsRaw, this.maps, "ind");
101
    this._indexSecondary(this.mapsRaw, this.maps, "name", "ind");
102
    this._indexSecondarySnake(this.mapsRaw, this.maps, "name", "ind");
103
    this._indexSecondaryPrefix(this.mapsRaw, this.maps, "alias", "ind", "");
104
    this._indexSecondaryPrefix(this.mapsRaw, this.maps, "symbol", "ind", "");
105
}
106
107
PokeredGameDB.prototype._autoIndex = function(rv, vName) {
108
    const v = this[vName] = [];
109
110
    this._indexPrimary(rv, v, "ind");
111
    this._indexSecondary(rv, v, "name", "ind");
112
    this._indexSecondarySnake(rv, v, "name", "ind");
113
}
114
115
PokeredGameDB.prototype._indexTextCodes = function() {
116
    this.textCodes = [];
117
118
    this._indexPrimary(this.textCodesRaw, this.textCodes, "ind");
119
    this._indexSecondaryPrefix(this.textCodesRaw, this.textCodes, "name", "ind", "text");
120
}
121
122
PokeredGameDB.prototype._processTowns = function() {
123
    this._autoIndex(this.townsRaw, "towns");
124
125
    this._iterate(this.towns, function(val) {
126
        this._processPrimitiveDBEntry(val, "name", this.maps);
127
    });
128
}
129
130
PokeredGameDB.prototype._processTrades = function() {
131
    this.trades = [];
132
    const tradesRaw = this._addIndexes(this.tradesRaw);
133
134
    this._iterate(tradesRaw, function(val) {
135
        this._processPrimitiveDBEntry(val, "give", monDB.pokemon);
136
        this._processPrimitiveDBEntry(val, "get", monDB.pokemon);
137
    });
138
139
    this._indexPrimary(tradesRaw, this.trades, "ind");
140
    this._indexSecondaryPrefix(tradesRaw, this.trades, "_give", "ind", "give");
141
    this._indexSecondaryPrefix(tradesRaw, this.trades, "_get", "ind", "get");
142
    this._indexSecondaryPrefix(tradesRaw, this.trades, "nickname", "ind", "name");
143
}
144
145
PokeredGameDB.prototype._processEventPokemon = function() {
146
    const eventPokemonRaw = this._addIndexes(this.eventPokemonRaw);
147
    this.eventPokemon = [];
148
149
    this._iterate(eventPokemonRaw, function(event) {
150
        this._processPrimitiveDBEntry(event, "pokemon", monDB.pokemon);
151
        this._processArrayDBEntry(event, "moves", monDB.moves);
152
        this.eventPokemon.push(event);
153
    });
154
}
155
156
PokeredGameDB.prototype._processHidden = function(v, name) {
157
    const hiddenRaw = this._addIndexes(v);
158
    this[name] = [];
159
160
    this._iterate(hiddenRaw, function(val) {
161
        this._processPrimitiveDBEntry(val, "map", this.maps);
162
        this[name].push(val);
163
    });
164
}
165
166
PokeredGameDB.prototype._processTrainers = function() {
167
    const trainersRaw = _.cloneDeep(this.trainersRaw);
168
    this.trainers = [];
169
    this.trainers.ally = [];
170
    this.trainers.opp = [];
171
172
    this._iterate(trainersRaw, function(val) {
173
        const oppVal = _.cloneDeep(val);
174
        oppVal.ind += 200;
175
        oppVal.opponent = true;
176
        trainersRaw.push(oppVal);
177
    });
178
179
    this._indexPrimary(trainersRaw, this.trainers, "ind");
180
181
    this._iterate(this.trainers, function(val) {
182
        if(val.ind < 200) {
183
            this.trainers[val.name] = val;
184
            this.trainers[_.snakeCase(val.name)] = val;
185
            this.trainers.ally[val.name] = val;
186
            this.trainers.ally[_.snakeCase(val.name)] = val;
187
            this.trainers.ally[val.ind] = val;
188
        } else {
189
            this.trainers["Opponent " + val.name] = val;
190
            this.trainers[_.snakeCase("Opponent " + val.name)] = val;
191
            this.trainers.opp[val.name] = val;
192
            this.trainers.opp[_.snakeCase(val.name)] = val;
193
            this.trainers.opp[val.ind] = val;
194
        }
195
    });
196
}
197
198
PokeredGameDB.prototype._indexScripts = function() {
199
    const scriptsRaw = this._addIndexes(this.scriptsRaw);
200
    const worldScriptsRaw = _.cloneDeep(this.worldScriptsRaw);
201
202
    for(let i = 0; i < scriptsRaw.length; i++) {
203
        delete worldScriptsRaw[i].name;
204
        _.assign(scriptsRaw[i], worldScriptsRaw[i]);
205
    }
206
207
    this.scripts = [];
208
209
    this._indexPrimary(scriptsRaw, this.scripts, "ind");
210
    this._indexSecondary(scriptsRaw, this.scripts, "name", "ind");
211
    this._indexSecondarySnake(scriptsRaw, this.scripts, "name", "ind");
212
    this._indexSecondaryPrefix(scriptsRaw, this.scripts, "varName", "ind", "");
213
}
214
215
PokeredGameDB.prototype.getRandomName = function() {
216
    return _.sample(this.randomNames);
217
}
218
219
PokeredGameDB.prototype.getRandomStarterCandidate = function() {
220
    return _.sample(this.starterCandidates);
221
}
222
223
PokeredGameDB.prototype.getRandomStarter = function() {
224
    return _.sample(this.starters);
225
}
226
227
// Assigns Index Numbering
228
PokeredGameDB.prototype._addIndexes = function(from) {
229
    let i = 0;
230
    const ret = [];
231
232
    this._iterate(_.cloneDeep(from), function(val) {
233
        val.ind = i;
234
        ret.push(val);
235
        i++;
236
    });
237
238
    return ret;
239
}
240
241
// This interlinks primitive data to an existing db entry
242
PokeredGameDB.prototype._processPrimitiveDBEntry = function(value, from, where) {
243
    if(value[from] !== undefined && value["_" + from] === undefined) {
244
        value["_" + from] = value[from];
245
        value[from] = where[value[from]];
246
    }
247
}
248
249
// This loops over a simple array if it exists and inter-links valid elements to other data
250
PokeredGameDB.prototype._processArrayDBEntry = function(value, from, where) {
251
    if(value[from] !== undefined && value['_' + from] === undefined) {
252
        value['_' + from] = _.cloneDeep(value[from]);
253
        for(let i = 0; i < value[from].length; i++) {
254
            const val = value[from][i];
255
            value[from][i] = where[val];
256
        }
257
    }
258
}
259
260
// Add several ways to access the raw data, nothings duplicated because it's just all copying
261
// by reference
262
PokeredGameDB.eventPokemonRaw = PokeredGameDB.prototype.eventPokemonRaw = eventPokemonRaw;
263
PokeredGameDB.eventsRaw = PokeredGameDB.prototype.eventsRaw = eventsRaw;
264
PokeredGameDB.hiddenCoinsRaw = PokeredGameDB.prototype.hiddenCoinsRaw = hiddenCoinsRaw;
265
PokeredGameDB.hiddenItemsRaw = PokeredGameDB.prototype.hiddenItemsRaw = hiddenItemsRaw;
266
PokeredGameDB.mapsRaw = PokeredGameDB.prototype.mapsRaw = mapsRaw;
267
PokeredGameDB.missablesRaw = PokeredGameDB.prototype.missablesRaw = missablesRaw;
268
PokeredGameDB.randomNamesRaw = PokeredGameDB.prototype.randomNamesRaw = randomNamesRaw;
269
PokeredGameDB.scriptsRaw = PokeredGameDB.prototype.scriptsRaw = scriptsRaw;
270
PokeredGameDB.spritesRaw = PokeredGameDB.prototype.spritesRaw = spritesRaw;
271
PokeredGameDB.starterCandidatesRaw = PokeredGameDB.prototype.starterCandidatesRaw = starterCandidatesRaw;
272
PokeredGameDB.startersRaw = PokeredGameDB.prototype.startersRaw = startersRaw;
273
PokeredGameDB.textCodesRaw = PokeredGameDB.prototype.textCodesRaw = textCodesRaw;
274
PokeredGameDB.townsRaw = PokeredGameDB.prototype.townsRaw = townsRaw;
275
PokeredGameDB.tradesRaw = PokeredGameDB.prototype.tradesRaw = tradesRaw;
276
PokeredGameDB.trainersRaw = PokeredGameDB.prototype.trainersRaw = trainersRaw;
277
PokeredGameDB.worldScriptsRaw = PokeredGameDB.prototype.worldScriptsRaw = worldScriptsRaw;
278